package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.*;
import net.starwifi.duobao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2015/12/9.
 */
@Controller("adminDBShareController")
@RequestMapping("/dbShare")
public class DBShareController extends BaseController {

    @Autowired
    DBShareService dbShareService;

    @Autowired
    DBTimesService dbTimesService;

    @Autowired
    MemberService memberService;

    @Autowired
    FileService fileService;

    @Autowired
    DBShareImageService dbShareImageService;

    /**
     * 添加
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String add(ModelMap model,Long dbTimesId) {
        DBTimes dbTimes=dbTimesService.find(dbTimesId);
        model.addAttribute("dbTimes",dbTimes);
        model.addAttribute("dbOrder",dbTimes.getDbOrder());
        model.addAttribute("member",memberService.find(dbTimes.getDbOrder().getMemberId()));
        List<DBShare> dbShareList=dbShareService.getListData("o.dbTimes.id=?1",new Object[]{dbTimesId});
        if(dbShareList!=null&&dbShareList.size()>=1){
            model.addAttribute("dbShare",dbShareList.get(0));
        }
        return "/dbShare/view";
    }

    /**
     * 添加
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Long id,Long dbTimesId,Long memberId,String title,String content,Boolean isShow,DBShare entity) {
        for (Iterator<DBShareImage> iterator = entity.getDbShareImages().iterator(); iterator.hasNext();) {
            DBShareImage productImage = iterator.next();
            if (productImage == null || productImage.isEmpty()) {
                iterator.remove();
                continue;
            }
        }
        DBTimes dbTimes=dbTimesService.find(dbTimesId);
        Member member=memberService.find(memberId);
        DBShare dbShare=null;
        if(id!=null){//save操作
            dbShare=dbShareService.find(id);
        }else{
            dbShare=new DBShare();
            dbShare.setProductId(dbTimes.getDbProduct().getId());
            dbShare.setProductName(dbTimes.getDbProduct().getName());
            dbShare.setCount(dbTimes.getDbOrder().getQuantity());
            dbShare.setDbTimes(dbTimes);
            dbShare.setMember(member);
        }
        dbShare.setDbShareImages(entity.getDbShareImages());
        dbShare.setTitle(title);
        dbShare.setContent(content);
        dbShare.setIsShow(isShow);
        for (DBShareImage shareImage : dbShare.getDbShareImages()) {
            dbShareImageService.build(shareImage,"/db_share_"+dbShare.getDbTimes().getTimesNo()+"/");
        }
        if(id==null){
            dbShareService.save(dbShare);
        }else{
            dbShareService.update(dbShare);
        }
        return "redirect:/dbShare/view?dbTimesId="+dbTimes.getId();
    }
}
